package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.APPMS.domain.Salesperson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Purchasevehicle;
import com.ruoyi.APPMS.service.IPurchasevehicleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购买车辆Controller
 *
 * @author wanghaoyu
 * @date 2023-07-03
 */
@RestController
@RequestMapping("/purchasevehicle/purchasevehicle")
@Api(tags = "车辆采购控制器")
public class PurchasevehicleController extends BaseController {
    @Autowired
    private IPurchasevehicleService purchasevehicleService;

    /**
     * 查询购买车辆列表
     */
    @PreAuthorize("@ss.hasPermi('purchasevehicle:purchasevehicle:list')")
    @GetMapping("/list")
    @ApiOperation("分页查询车辆采购信息")
    public TableDataInfo list(Purchasevehicle purchasevehicle) {
        startPage();
        List<Purchasevehicle> list = purchasevehicleService.list(purchasevehicle);
        return getDataTable(list);
    }

    /**
     * 导出购买车辆列表
     */
    @PreAuthorize("@ss.hasPermi('purchasevehicle:purchasevehicle:export')")
    @Log(title = "购买车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出车辆采购信息")
    public void export(HttpServletResponse response, Purchasevehicle purchasevehicle) {
        List<Purchasevehicle> list = purchasevehicleService.list(purchasevehicle);
        ExcelUtil<Purchasevehicle> util = new ExcelUtil<Purchasevehicle>(Purchasevehicle.class);
        util.exportExcel(response, list, "购买车辆数据");
    }

    /**
     * 获取购买车辆详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchasevehicle:purchasevehicle:query')")
    @GetMapping(value = "/{vehicleid}")
    @ApiOperation("展示一条车辆采购信息")
    public AjaxResult getInfo(@PathVariable("vehicleid") Long vehicleid) {
        return success(purchasevehicleService.listByid(vehicleid));
    }

    /**
     * 新增购买车辆
     */
    @PreAuthorize("@ss.hasPermi('purchasevehicle:purchasevehicle:add')")
    @Log(title = "购买车辆", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加车辆购买信息")
    public AjaxResult add(@RequestBody Purchasevehicle purchasevehicle) {
        purchasevehicle.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(purchasevehicleService.save(purchasevehicle));
    }

    /**
     * 修改购买车辆
     */
    @PreAuthorize("@ss.hasPermi('purchasevehicle:purchasevehicle:edit')")
    @Log(title = "购买车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("创建订单")
    public AjaxResult edit(@RequestBody Purchasevehicle purchasevehicle) {
        purchasevehicleService.createOrder1(purchasevehicle);
        return toAjax(purchasevehicleService.createOrder2(purchasevehicle));
    }

    /**
     * 删除购买车辆
     */
    @PreAuthorize("@ss.hasPermi('purchasevehicle:purchasevehicle:remove')")
    @Log(title = "购买车辆", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vehicleids}")
    @ApiOperation("删除车辆购买信息")
    public AjaxResult remove(@PathVariable List<Long> vehicleids) {
        return toAjax(purchasevehicleService.removeByIds(vehicleids));
    }
}
